/*
 *
 * Copyright 2011 OpenAjax Alliance
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

Components.utils.import("resource://oaa-cache-inspector/cache-inspector-common.js");

/* ---------------------------------------------------------------- */
/*                  Initialize namespace properties                 */
/* ---------------------------------------------------------------- */

if (typeof OAA_CI == "undefined") {
  var OAA_CI = {};
}

/**
 * @constructor SidebarView
 *
 * @memberOf OAA_CI
 *
 * @desc Instantiate a sidebar view object.
 *
 * @param treechildren_id {String} - XUL ID of treechildren assoc. with view
 *
 * @param {Boolean}     isInitialized      - True if the view is visible and popluated
 * @param {XULElement}  tab                - XUL element node of the XUL tab element in a tab panel
 * @param {XULElement}  tree               - XUL element node of the XUL tree element
 * @param {Object}      cache              - The cache being used for this view
 * @param {String}      tcid               - ID for the XUL treechildren element of a tree
 *
 * @param {PropertiesTabPanel} properties  - reference to a properties panel  
 */

OAA_CI.SidebarView = function (treechildren_id) {
  this.isInitialized    = false;
  this.tab              = null;
  this.tree             = null;
  this.cache            = null;
  this.tcid             = treechildren_id;
  this.properties       = null;  
};

/**
 * @method addProperties
 *
 * @memberOf OAA_CI.SidebarView
 *
 * @desc Add a properties panel to the view
 *
 * @param {Object}  properties  - Additional view of information related to a selection 
 */

OAA_CI.SidebarView.prototype.addProperties = function (properties) {
  this.properties  = properties;
};

/**
 * @method getTreechildren
 *
 * @memberOf OAA_CI.SidebarView
 *
 * @desc Return the XUL treechildren associated with this view.
 */

OAA_CI.SidebarView.prototype.getTreechildren = function () {
  return document.getElementById(this.tcid);
};

/**
 * @method showCacheItemProperties
 *
 * @memberOf OAA_CI.SidebarView
 *
 * @desc Retrieve an item from associated cache and display its properties.
 *
 * @param {XUL tree} tree - XUL tree with selected element.
 * @param {int} index - Tree row index of selected element.
 *
 * @return {object | null}  Cache item that is currently selected 
 */

OAA_CI.SidebarView.prototype.showCacheItemProperties = function (index) {

  var cache_id = OAA_CI.getCacheIdFromTree(this.tree, index);
  var item = this.cache.getItemByCacheId(cache_id);
  
  if (item && this.properties) this.properties.updateProperties(item);
  
  return item;
  
};

/**
 * @method updateItemSelectionDisplay
 *
 * @memberOf OAA_CI.SidebarView
 *
 * @desc Determine whether a cache item is selected in the XUL tree;
 * if so, show its properties and highlight it on the page; otherwise
 * clear the cache properties panel and the page highlighting.
 *
 * @return {object | null}  Cache item that is currently selected 
 */

OAA_CI.SidebarView.prototype.updateItemSelectionDisplay = function () {

  var item = null;
  
  var index = this.tree.currentIndex;
  if (this.tree.view.selection.isSelected(index))
    item = this.showCacheItemProperties(index);
  else
    if (this.properties) this.properties.clearProperties();

  return item;
};
